home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / python / Lib / os.py < prev    next >
Encoding:
Text File  |  1994-04-01  |  1.9 KB  |  70 lines  |  [TEXT/R*ch]

  1. # os.py -- either mac, dos or posix depending on what system we're on.
  2.  
  3. # This exports:
  4. # - all functions from either posix or mac, e.g., os.unlink, os.stat, etc.
  5. # - os.path is either module posixpath or macpath
  6. # - os.name is either 'posix' or 'mac'
  7. # - os.curdir is a string representing the current directory ('.' or ':')
  8. # - os.pardir is a string representing the parent directory ('..' or '::')
  9. # - os.sep is the (or a most common) pathname separator ('/' or ':')
  10.  
  11. # Programs that import and use 'os' stand a better chance of being
  12. # portable between different platforms.  Of course, they must then
  13. # only use functions that are defined by all platforms (e.g., unlink
  14. # and opendir), and leave all pathname manipulation to os.path
  15. # (e.g., split and join).
  16.  
  17. _osindex = {
  18.       'posix': ('.', '..', '/'),
  19.       'dos':   ('.', '..', '\\'),
  20.       'mac':   (':', '::', ':'),
  21.       'nt':   ('.', '..', '\\'),
  22. }
  23.  
  24. import sys
  25. for name in _osindex.keys():
  26.     if name in sys.builtin_module_names:
  27.         curdir, pardir, sep = _osindex[name]
  28.         exec 'from %s import *' % name
  29.         exec 'import %spath' % name
  30.         exec 'path = %spath' % name
  31.         exec 'del %spath' % name
  32.         try:
  33.             exec 'from %s import _exit' % name
  34.         except ImportError:
  35.             pass
  36.         break
  37. else:
  38.     del name
  39.     raise ImportError, 'no os specific module found'
  40.  
  41. def execl(file, *args):
  42.     execv(file, args)
  43.  
  44. def execle(file, *args):
  45.     env = args[-1]
  46.     execve(file, args[:-1], env)
  47.  
  48. def execlp(file, *args):
  49.     execvp(file, args)
  50.  
  51. def execvp(file, args):
  52.     if '/' in file:
  53.         execv(file, args)
  54.         return
  55.     ENOENT = 2
  56.     if environ.has_key('PATH'):
  57.         import string
  58.         PATH = string.splitfields(environ['PATH'], ':')
  59.     else:
  60.         PATH = ['', '/bin', '/usr/bin']
  61.     exc, arg = (ENOENT, 'No such file or directory')
  62.     for dir in PATH:
  63.         fullname = path.join(dir, file)
  64.         try:
  65.             execv(fullname, args)
  66.         except error, (errno, msg):
  67.             if errno != ENOENT:
  68.                 exc, arg = error, (errno, msg)
  69.     raise exc, arg
  70.